/* 
 * 定义itfreer.arcgis.measure测量模块
 */
(function(window, angular, undefined){
	/* 
	 * 定义ArcGIS测量模块
	 */
	var itfreerArcgisMeasureModule = angular.module('itfreer.arcgis.measure', ['itfreer.arcgis.draw', 'itfreer.arcgis.geometryservice']);

	/* 
	 * 定义ArcGIS测量模块
	 */
	itfreerArcgisMeasureModule.factory('$itfreer_arcgis_measure', 
		function($rootScope, $gisObject, $itfreer_arcgis_core, $itfreer_core, 
			$itfreer_arcgis_draw, $itfreer_arcgis_geometryservice) {
			$itfreer_arcgis_measure = {

				/* 
				 * 显示测量距离
				 */
				onLengthsComplete : function(result, geometry){
				   	if (result && $gisObject.map.graphics != null) {

				   		var guid = $itfreer_core.getGUID();

				   		// 先添加绘制图形
						var symbol = $itfreer_arcgis_draw.getLineSymbol();
					    var graphic = new esri.Graphic(geometry, symbol);
					    if(!graphic.attributes){
							graphic.attributes = {};
						}
						graphic.attributes.$isLengthsComplete = true;
						graphic.attributes.$guid = guid;
					    $gisObject.map.graphics.add(graphic);

					    // 添加结果
						var resultStr = Number(result.lengths[0]).toFixed(3) + " 公里(关闭)";
						var font = new esri.symbol.Font("13px",
								esri.symbol.Font.STYLE_NORMAL,
								esri.symbol.Font.VARIANT_NORMAL,
								esri.symbol.Font.WEIGHT_BOLDER);
						var textSymbol = new esri.symbol.TextSymbol(resultStr, font, new dojo.Color([0, 0, 0]));
						var _center = $itfreer_arcgis_core.getCenter(geometry);
						var labelPointGraphic = new esri.Graphic(_center, textSymbol);
						if(!labelPointGraphic.attributes){
							labelPointGraphic.attributes = {};
						}
						labelPointGraphic.attributes.$isLengthsComplete = true;
						labelPointGraphic.attributes.$guid = guid;
						$gisObject.map.graphics.add(labelPointGraphic);
						
						// 启用map.graphics的鼠标事件
					    $gisObject.map.graphics.enableMouseEvents();
				       	dojo.connect($gisObject.map.graphics, "onClick", function (evt){
				    		var graphic = evt.graphic;
				    		if(!graphic.attributes){
					    		return;
					    	}

					    	if(graphic.attributes.$isLengthsComplete 
					    		&& graphic.attributes.$isLengthsComplete==true){
					    		var _guid = graphic.attributes.$guid;
					    		$gisObject.map.graphics.remove(graphic);

						    	//遍历地图的graphics查找guid一样的内容
							   	for(var i=0;i<$gisObject.map.graphics.graphics.length;i++){
							      	var cGrapphic=$gisObject.map.graphics.graphics[i];
							      	if(cGrapphic.attributes
							      		&& cGrapphic.attributes.$isLengthsComplete 
							      		&& cGrapphic.attributes.$isLengthsComplete==true
							      		&& cGrapphic.attributes.$guid==_guid){
							         	$gisObject.map.graphics.remove(cGrapphic);
							         	break;
							      	}
							   	}
					    	}
				    	});
					}
				},

				/* 
				 * 显示测量面积
				 */
				onAreasAndLengthsComplete : function(result, geometry){
				   	if (result && $gisObject.map.graphics != null) {

				   		var guid = $itfreer_core.getGUID();

				   		// 先添加绘制图形
					    var symbol = $itfreer_arcgis_draw.getFillSymbol();
					    var graphic = new esri.Graphic(geometry, symbol);
					    if(!graphic.attributes){
							graphic.attributes = {};
						}
						graphic.attributes.$isAreasAndLengthsComplete = true;
						graphic.attributes.$guid = guid;
					    $gisObject.map.graphics.add(graphic);

					    // 先添加绘制结果
						var resultStr = Number(result.areas[0]).toFixed(3) + "平方公里(单击关闭)";
						var font = new esri.symbol.Font("13px",
								esri.symbol.Font.STYLE_NORMAL,
								esri.symbol.Font.VARIANT_NORMAL,
								esri.symbol.Font.WEIGHT_BOLDER);
						var textSymbol = new esri.symbol.TextSymbol(resultStr, font,
								new dojo.Color([0, 0, 0]));
						var _center = $itfreer_arcgis_core.getCenter(geometry);
						var labelPointGraphic = new esri.Graphic(_center, textSymbol);
						if(!labelPointGraphic.attributes){
							labelPointGraphic.attributes = {};
						}
						labelPointGraphic.attributes.$isAreasAndLengthsComplete = true;
						labelPointGraphic.attributes.$guid = guid;
						$gisObject.map.graphics.add(labelPointGraphic);

						// 启用map.graphics的鼠标事件
					    $gisObject.map.graphics.enableMouseEvents();
				       	dojo.connect($gisObject.map.graphics, "onClick", function (evt){
				    		var graphic = evt.graphic;
				    		if(!graphic.attributes){
					    		return;
					    	}

					    	if(graphic.attributes.$isAreasAndLengthsComplete 
					    		&& graphic.attributes.$isAreasAndLengthsComplete==true){
					    		var _guid = graphic.attributes.$guid;
					    		$gisObject.map.graphics.remove(graphic);

						    	//遍历地图的graphics查找guid一样的内容
							   	for(var i=0;i<$gisObject.map.graphics.graphics.length;i++){
							      	var cGrapphic=$gisObject.map.graphics.graphics[i];
							      	if(cGrapphic.attributes
							      		&& cGrapphic.attributes.$isAreasAndLengthsComplete 
							      		&& cGrapphic.attributes.$isAreasAndLengthsComplete==true
							      		&& cGrapphic.attributes.$guid==_guid){
							         	$gisObject.map.graphics.remove(cGrapphic);
							         	break;
							      	}
							   	}
					    	}
				    	});
					}
				},

				/**
				 * 测试距离
				 * 
				 * @function {public} ?
				 */
				distance : function() {
					var drawGeometry = null;
					$itfreer_arcgis_draw.onDrawEndEventFunction = function(geometry){
						drawGeometry = geometry;
						$itfreer_arcgis_draw.deDraw();

					    // 如果是地理坐标系统，则先转换为投影坐标
					    if($gisObject.mapisdlwkid){
					    	$itfreer_arcgis_geometryservice.project([geometry], $gisObject.tywkid);
					    }else{
					    	$itfreer_arcgis_geometryservice.lengths([geometry]);
					    }
					};

					$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction = function(graphics){
						$itfreer_arcgis_geometryservice.lengths([graphics[0]]);
					};
					
					$itfreer_arcgis_geometryservice.onLengthsCompleteEventFunction = function(result){
						$itfreer_arcgis_measure.onLengthsComplete(result, drawGeometry);
					};

					$itfreer_arcgis_geometryservice.onSimplifyCompleteEventFunction = null;
					$itfreer_arcgis_geometryservice.onAreasAndLengthsCompleteEventFunction = null;
					$itfreer_arcgis_geometryservice.onBufferCompleteEventFunction = null;

					$itfreer_arcgis_draw.drawPolyline();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/ruler.cur),auto");
					}
				},

				/**
				 * 测试面积
				 * 
				 * @function {public} ?
				 */
				area : function() {
					var drawGeometry = null;
					$itfreer_arcgis_draw.onDrawEndEventFunction = function(geometry){
						$itfreer_arcgis_draw.deDraw();
						drawGeometry = geometry;

					    // 如果是地理坐标系统，则先转换为投影坐标
					    if($gisObject.mapisdlwkid){
					    	$itfreer_arcgis_geometryservice.project([geometry], $gisObject.tywkid);
					    }else{
					    	// 如果为面类型需要先进行simplify操作在进行面积测算
						    $itfreer_arcgis_geometryservice.simplify([geometry]);
					    }
					};

					$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction = function(graphics){
						// 如果为面类型需要先进行simplify操作在进行面积测算
					    $itfreer_arcgis_geometryservice.simplify([graphics[0]]);
					};

					$itfreer_arcgis_geometryservice.onSimplifyCompleteEventFunction = function(graphics){
						// 进行面积测量
						$itfreer_arcgis_geometryservice.areasAndLengths(graphics);
					};

					$itfreer_arcgis_geometryservice.onAreasAndLengthsCompleteEventFunction = function(result){
						$itfreer_arcgis_measure.onAreasAndLengthsComplete(result, drawGeometry);
					};

					$itfreer_arcgis_geometryservice.onLengthsCompleteEventFunction = null;
					$itfreer_arcgis_geometryservice.onBufferCompleteEventFunction = null;

					$itfreer_arcgis_draw.drawPolygon();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/ruler.cur),auto");
					}
				}
			};
			for(f in $itfreer_arcgis_measure){
				$rootScope[f] = $itfreer_arcgis_measure[f];
			}
			return $itfreer_arcgis_measure;
		}
	);

	/* 
	 * 加载服务器
	 */
	itfreerArcgisMeasureModule.run(function($rootScope, $itfreer_arcgis_measure) {
	  	
	});
}(window, angular));